<?php

namespace app\store\model;

use app\common\model\GoodsSku as GoodsSkuModel;
use app\store\model\appointment\Type;
use think\Db;

/**
 * 商品规格模型
 * Class GoodsSku
 * @package app\store\model
 */
class GoodsSku extends GoodsSkuModel
{
    /**
     * 批量添加商品sku记录
     * @param $goods_id
     * @param $spec_list
     * @return array|false
     * @throws \Exception
     */
    public function addSkuList($goods_id, $spec_list)
    {
        $data = [];
        foreach ($spec_list as $item) {
            $data[] = array_merge($item['form'], [
                'spec_sku_id' => $item['spec_sku_id'],
                'goods_id' => $goods_id,
                'wxapp_id' => self::$wxapp_id,
            ]);
        }
        var_dump($data);die;
        return $this->allowField(true)->saveAll($data);
    }

    /**
     * 添加商品规格关系记录
     * @param $goods_id
     * @param $spec_attr
     * @return array|false
     * @throws \Exception
     */
    public function addGoodsSpecRel($goods_id, $spec_attr)
    {
        $data = [];
        array_map(function ($val) use (&$data, $goods_id) {
            array_map(function ($item) use (&$val, &$data, $goods_id) {
                $data[] = [
                    'goods_id' => $goods_id,
                    'spec_id' => $val['group_id'],
                    'spec_value_id' => $item['item_id'],
                    'wxapp_id' => self::$wxapp_id,
                ];
            }, $val['spec_items']);
        }, $spec_attr);
        $model = new GoodsSpecRel;
        return $model->saveAll($data);
    }

    /**
     * 移除指定商品的所有sku
     * @param $goods_id
     * @return int
     */
    public function removeAll($goods_id)
    {
        $model = new GoodsSpecRel;
        $model->where('goods_id','=', $goods_id)->delete();

        $list = $this->where('goods_id','=', $goods_id)->select();
        foreach ($list as $val)
        {
            $data['goods_sku_id'] = $val['goods_sku_id'];
            $data['goods_id'] = $val['goods_id'];
            $data['image_id'] = $val['image_id'];
            $data['goods_no'] = $val['goods_no'];
            $data['goods_price'] = $val['goods_price'];
            $data['line_price'] = $val['line_price'];
            $data['type_id'] = $val['type_id'];
            $data['goods_sku_name'] = $val['goods_sku_name'];
            $data['wxapp_id'] = $val['wxapp_id'];
//            $data['create_time'] = $val['create_time'];
            $check =  Db::name('goods_sku_copy')->where(['goods_sku_id'=>$val['goods_sku_id']])->find();
            if(!$check){
                Db::name('goods_sku_copy')->insert($data);
            }
        }
        return $this->where('goods_id','=', $goods_id)->delete();
    }

    /*
     * 获取当前商品的预约的类型
     */
    public function getGoodsType($sku_id)
    {

        $type_id = GoodsSkuModel::where(['goods_sku_id'=>$sku_id,'wxapp_id'=>self::$wxapp_id])->value('type_id');

        if(!$type_id){
            return false;
        }else{
            $list = Type::detail($type_id);
            $data = ['name'=>$list['name'],'type_id'=>$list['type_id']];
            return $data;
        }

    }

}
